home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Utilities / StdIFmIO.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  5.3 KB  |  217 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        StdIFmIO.cpp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Owned by:    Nick Pilch
  7.  
  8.     Copyright:    
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     9/17/96    RA        1301110: ODSetIconFamilyProp throws
  13.                                     deleting other platform's icons
  14.          <2>    .04.1996    NP        Change to allow ObjectMaster to parse. No
  15.                                     code changes.
  16.  
  17.     To Do:
  18. */
  19.  
  20. /*
  21.     File:        StdIFmIO.cpp
  22.  
  23.     Contains:    functions for read/writing standard IconFamily from/to storage units.
  24.                 This file is Macintosh-specific.
  25.  
  26.     Owned by:    Tantek Çelik, Jens Alfke
  27.  
  28.     Copyright:    © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
  29.  
  30.     To Do:
  31.         1. Ensure that usage of functions to write substreams works (nil prop & val).
  32.     In Progress:
  33.         
  34. */
  35.  
  36.  
  37. #ifndef _EXCEPT_
  38. #include <Except.h>
  39. #endif
  40.  
  41. #ifndef _ODDEBUG_
  42. #include "ODDebug.h"
  43. #endif
  44.  
  45. #ifndef _ODMEMORY_
  46. #include <ODMemory.h>
  47. #endif
  48.  
  49. #ifndef SOM_ODStorageUnit_xh
  50. #include <StorageU.xh>
  51. #endif
  52.  
  53. #ifndef SOM_ODStorageUnitView_xh
  54. #include <SUView.xh>
  55. #endif
  56.  
  57. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  58. #include <StdTypes.xh>
  59. #endif
  60.  
  61. #ifndef _STDTYPIO_
  62. #include "StdTypIO.h"
  63. #endif
  64.  
  65. #ifndef _STORUTIL_
  66. #include <StorUtil.h>
  67. #endif
  68.  
  69. #ifndef _TEMPOBJ_
  70. #include "TempObj.h"
  71. #endif
  72.  
  73. #ifndef __ICONS__
  74. #include <Icons.h>
  75. #endif
  76.  
  77. //==============================================================================
  78. // Constants
  79. //==============================================================================
  80.  
  81. static const int kNIconTypes = 8;
  82. static const ResType    kIconType[kNIconTypes] 
  83.                     = {'ics#','ics4','ics8',     0,     0,'ICN#','icl4','icl8'};
  84. static const short        kIconSize[kNIconTypes]
  85.                     = {    64,   128,   256,   512,  1024,   256,   512,  1024};
  86.  
  87.  
  88. //==============================================================================
  89. // IconFamily
  90. //==============================================================================
  91.  
  92.  
  93. const ODULong kAllMacIconsMask = 0x00E7;        // 16x16 and 32x32 / 1,4,8 bits deep 
  94.  
  95. //    If there is no value at the passed in prop & val then kODNULL is returned.
  96. //    This is consistent with the other routines in StdTypIO which return pointers.
  97. //    Other StdTypIO routines which actually return the value gotten, WARN if no value
  98. //    is there.
  99. //    The reason is that there may be clients of ODIconFamily which more easily 
  100. //    handle the flow of control if it just returns kODNULL instead of THROWing. -Tantek
  101.  
  102. ODIconFamily
  103. ODGetIconFamilyProp(Environment* ev, ODStorageUnit* su, ODPropertyName prop, 
  104.                     ODValueType val, ODULong iconMask)
  105. {
  106.     if( val == kODIconFamily )
  107.         val = kODIconFamilyMac;        // If only kODIconFamily specified, use platform type
  108.         
  109.     if (ODSUExistsThenFocus(ev, su, prop, val))
  110.     {
  111.         iconMask &= kAllMacIconsMask;
  112.         
  113.         ODULong which;
  114.         ODSize pos = sizeof(which);
  115.         StorageUnitGetValue(su, ev, sizeof(which), &which);        
  116.             // Which icons have data?
  117.         
  118.         if( (which & iconMask) == 0 )
  119.             return kODNULL;
  120.         
  121.         ODIconFamily iconFamily;
  122.         THROW_IF_ERROR( NewIconSuite((Handle*)&iconFamily) );
  123.         
  124.         ODHandle h=kODNULL; ODVolatile(h);
  125.         TRY{
  126.             for( int i=0; i<kNIconTypes; i++ )
  127.                 if( which & (1<<i) ) {
  128.                     ODSize size = kIconSize[i];
  129.                     pos += size;
  130.                     if( iconMask & (1<<i) ) {            // Read icon:
  131.                         h = ODNewHandle(size);
  132.                         StorageUnitGetValue(su, ev,size, ODLockHandle(h));
  133.                         ODUnlockHandle(h);
  134.                         THROW_IF_ERROR( AddIconToSuite((Handle)h,(Handle)iconFamily,kIconType[i]) );
  135.                         h = kODNULL;
  136.                     } else
  137.                         su->SetOffset(ev,pos);            // Don't need this icon
  138.                 }
  139.         }CATCH_ALL{
  140.             ODDisposeHandle(h);
  141.             DisposeIconSuite((Handle)iconFamily,kODTrue);
  142.             RERAISE;
  143.         }ENDTRY
  144.         
  145.         return iconFamily;
  146.     }
  147.     else
  148.         return kODNULL;
  149. }
  150.  
  151.                         
  152. #define SKIPOMPARSE
  153. #ifdef SKIPOMPARSE
  154. static const char* const kODIconFamilyBaseString = kODIconFamily ":";
  155. #else
  156. static const char* const kODIconFamilyBaseString = "^^^^correct def.^^^^^";
  157. #endif
  158.  
  159. void
  160. ODSetIconFamilyProp(Environment* ev, ODStorageUnit* su, ODPropertyName prop, 
  161.                     ODValueType val, ODIconFamily iconFamily,
  162.                     ODBoolean removeOtherPlatformIcons)
  163. {
  164.     ODSUForceFocus(ev, su, prop, val);
  165.  
  166.     ODBoolean wholeValue = (prop!=kODNULL || val!=kODNULL);
  167.     
  168.     // See which icons exist in the family:
  169.     ODULong which = 0;
  170.     Handle icon;
  171.     if( iconFamily != kODNULL )
  172.         for( int i=0; i<kNIconTypes; i++ )
  173.             if( kIconType[i] && GetIconFromSuite(&icon,(Handle)iconFamily,kIconType[i])==noErr && icon )
  174.                 which |= (1<<i);
  175.             
  176.     if( which==0 && wholeValue) 
  177.     {
  178.         // If there are no icons, remove the value:
  179.         su->Remove(ev);
  180.     }
  181.     else 
  182.     {
  183.         su->SetOffset(ev,0);
  184.         ODSize size = su->GetSize(ev);
  185.         su->DeleteValue(ev,size);
  186.         
  187.         StorageUnitSetValue(su, ev, sizeof(which), &which);
  188.         for( int i=0; i<kNIconTypes; i++ )
  189.             if( kIconType[i] && GetIconFromSuite(&icon,(Handle)iconFamily,kIconType[i])==noErr && icon ) 
  190.             {
  191.                 TRY{
  192.                     ODLockHandle((ODHandle)icon);
  193.                     StorageUnitSetValue(su, ev, kIconSize[i], (ODValue)(*icon));
  194.                 }CATCH_ALL{
  195.                     ODUnlockHandle((ODHandle)icon);
  196.                     RERAISE;
  197.                 }ENDTRY
  198.                 ODUnlockHandle((ODHandle)icon);
  199.             }
  200.     }
  201.     
  202.     if( removeOtherPlatformIcons ) {
  203.         ODULong nValues = su->CountValues(ev);
  204.         ODULong index = 1;
  205.         while( nValues-- != 0 ) {
  206.             su->Focus(ev,kODNULL,kODPosSame, kODNULL,index,0);    // Focus to value by index
  207.             TempODValueType type = su->GetType(ev);
  208.             if( strncmp(type,kODIconFamilyBaseString,strlen(kODIconFamilyBaseString)) == 0
  209.                             && strcmp(type,val) != 0 )
  210.                 su->Remove(ev);
  211.             else
  212.                 index++;
  213.         }
  214.     }
  215. }
  216.                             
  217.